<%@ page import="com.sxj.web.entity.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: User
  Date: 2024/3/13
  Time: 18:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
<%
    // 模拟数据
    pageContext.setAttribute("msg","Hello pageContext");
    request.setAttribute("msg","Hello request");
    session.setAttribute("msg","Hello session");
    application.setAttribute("msg","Hello application");

    request.setAttribute("name","admin");

    User user = new User(1,"admin","123456","13812345678","江苏-南京");
    User user2 = new User(2,"alice","123456","13812345678","江苏-南京");
    User user3 = new User(3,"jack","123456","13812345678","江苏-南京");
    request.setAttribute("user",user);
    List<User> users = new ArrayList<User>();
    users.add(user);
    users.add(user2);
    users.add(user3);
    request.setAttribute("users",users);

    Map<String,User> map = new HashMap<String,User>();
    // key必须以英文字母开头
    map.put("u1",user);
    map.put("u2",user2);
    map.put("u3",user3);
    request.setAttribute("map",map);
    request.setAttribute("age",20);
%>
<h2>访问作用域</h2>
<!--
    访问作用域时,通过作用域对象.key的方式进行获取
-->
pageContext:${pageScope.msg}<br>
request:${requestScope.msg}<br>
session:${sessionScope.msg}<br>
application:${applicationScope.msg}<br>
<!--
    如果不存在冲突的key,可以直接使用key进行获取
-->
name:${name}<br>
<!--
    如果多个作用域存在相同的key
    根据作用域大小由小到大进行搜索
    pageContext->request->session->application的顺序
    在某个作用域找到数据之后直接返回,停止搜索
    如果四大作用域中均没有对应数据,则什么都不显示
-->
msg:${msg2}
<h2>访问应用名</h2>
应用名:
<%
    HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
    String path = req.getContextPath();
    out.print(path);
%><br>
<!--
    在表达式中,如果存在对象.getXxx()方法时,可以直接使用对象.xxx表示
-->
应用名:${pageContext.request.contextPath}
<h2>访问对象</h2>
user:${user.id}--${user.username}--${user.password}--${user.phone}--${user.address}
<h2>访问List集合</h2>
${users[0].id}--${users[1].id}--${users[2].id}
<h2>访问Map集合</h2>
${map.u1.id}--${map['u2'].id}
<h2>访问请求参数</h2>
id:${param.id}
<h2>运算</h2>
3+2=${3+2}<br>
3-2=${3-2}<br>
3*2=${3*2}<br>
3/2=${3/2}<br>
3%2=${3%2}<br>
三目:${age < 18 ? "未成年" : "成年"}<br>
user是否为空:${empty user}<br>
emp是否为空:${empty emp}<br>
值的比较:${1 == 1}<br>
字符串比较:${1 eq "1"}<br>
字符串比较:${1 == "1"}<br>
与:${3 > 2 and 2 > 3}<br>
或:${3 > 2 or 2 > 3}<br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>
